package com.qiyu.tech.socket.proxy;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * 基于bio实现一个http代理
 * @Author linhao
 * @Date created in 6:59 下午 2021/3/10
 */
public class SocketProxy {

    private Socket socket;

    public SocketProxy(Socket socket) {
        this.socket = socket;
    }

    class HttpRequestProperties{
        private String requestType;

        private String requestUrl;

        private String httpProtocol;

        private String host;

        private String connection;

        public String getRequestType() {
            return requestType;
        }

        public void setRequestType(String requestType) {
            this.requestType = requestType;
        }

        public String getRequestUrl() {
            return requestUrl;
        }

        public void setRequestUrl(String requestUrl) {
            this.requestUrl = requestUrl;
        }

        public String getHttpProtocol() {
            return httpProtocol;
        }

        public void setHttpProtocol(String httpProtocol) {
            this.httpProtocol = httpProtocol;
        }

        public String getHost() {
            return host;
        }

        public void setHost(String host) {
            this.host = host;
        }

        public String getConnection() {
            return connection;
        }

        public void setConnection(String connection) {
            this.connection = connection;
        }
    }

    public void action() throws IOException {
        byte[] clientInputData = new byte[1024];
        InputStream in = this.socket.getInputStream();
        in.read(clientInputData);
        String input = new String(clientInputData);
        HttpRequestProperties httpRequestProperties = convertHttpRequestProperties(input);
        String httpUrl = httpRequestProperties.getRequestUrl().trim().replaceFirst("/","");
        System.out.println(httpUrl);
        URL url = new URL(httpUrl);
        System.out.println(url.getProtocol());
        HttpURLConnection action = (HttpURLConnection) url.openConnection();
        Map<String, List<String>> result = action.getRequestProperties();
        InputStream sis = action.getInputStream();
        OutputStream cos = socket.getOutputStream();
        int length;
        byte bytes[] = new byte[1024];
        while (true) {
            try {
                if ((length = sis.read(bytes)) > 0) {
                    cos.write(bytes, 0, length);
                    System.out.println(new String(bytes));
                    cos.flush();
                } else if (length < 0) {
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
        }
        socket.close();
        cos.close();
        sis.close();
    }

    /**
     * 转换http请求
     *
     * @param input
     * @return
     */
    private  HttpRequestProperties convertHttpRequestProperties(String input){
        //监听http请求
        String[] items = input.split("\r\n");
        HttpRequestProperties httpRequestProperties = new HttpRequestProperties();
        httpRequestProperties.setRequestType(items[0].substring(0,items[0].indexOf(" ")));
        httpRequestProperties.setRequestUrl(items[0].substring(items[0].indexOf(" "),items[0].indexOf("HTTP")));
        httpRequestProperties.setHttpProtocol(items[0].substring(items[0].indexOf("H")));
        return httpRequestProperties;
    }

    public static void main(String args[]) throws Exception {
        ServerSocket serverSocket = new ServerSocket(8081);
        while (true) {
            Socket socket = serverSocket.accept();
            SocketProxy server = new SocketProxy(socket);
            server.action();
        }
    }
}
